package com.zeny.coding.controller.admin.business;


import com.zeny.coding.constants.CourseConstants;
import com.zeny.coding.dto.PageDto;
import com.zeny.coding.dto.Response;
import com.zeny.coding.dto.SectionDto;
import com.zeny.coding.service.SectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.xml.bind.ValidationException;

/**
 * <h2>小节</h2>
 *
 * @Author Zeny
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/business/admin/section")
public class SectionController {

    @Autowired
    private SectionService sectionService;

    @PostMapping("/list/{courseId}/{chapterId}")
    public Response list(@RequestBody PageDto<SectionDto> pageDto,
                         @PathVariable("courseId") String courseId,
                         @PathVariable("chapterId") String chapterId) {
        pageDto.check();
        sectionService.list(pageDto, courseId, chapterId);
        return new Response().success().data(pageDto).message(CourseConstants.QUERY_SUCCESS);
    }

    @PostMapping("/save")
    public Response save(@RequestBody SectionDto sectionDto) throws ValidationException {
        sectionDto.isValidForSave();
        this.sectionService.save(sectionDto);
        return new Response().success().message(CourseConstants.SAVE_SUCCESS);
    }

    @PutMapping("/update")
    public Response update(@RequestBody SectionDto sectionDto) throws ValidationException {
        sectionDto.isValidForUpdate();
        this.sectionService.update(sectionDto);
        return new Response().success().message(CourseConstants.UPDATE_SUCCESS);
    }

    @GetMapping("/findById/{id}")
    public Response findById(@PathVariable("id") String id) {
        if (StringUtils.isEmpty(id)) {
            return new Response().fail().message(CourseConstants.REQUEST_PARAMS_ERROR);
        }
        SectionDto sectionDto = this.sectionService.findById(id);
        return new Response()
                .success()
                .data(sectionDto)
                .message(CourseConstants.QUERY_SUCCESS);
    }

    @DeleteMapping("/deleteById/{id}")
    public Response deleteById(@PathVariable("id") String id) {
        if (StringUtils.isEmpty(id)) {
            return new Response().fail().message(CourseConstants.REQUEST_PARAMS_ERROR);
        }
        this.sectionService.deleteById(id);
        return new Response().success().message(CourseConstants.DELETE_SUCCESS);
    }
}
